﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace Techappy.Silverlight.Toolkit
{
    public class MyPanel : Panel
    {
        public MyPanel()
        {
            this.MouseMove += new MouseEventHandler(MyPanel_MouseMove);
        }

        void MyPanel_MouseMove(object sender, MouseEventArgs e)
        {
            Point point = e.GetPosition(this);
            if (point.X <= center.X)
            {
                if (relatePoint.X <= center.X) return;

                double xx = center.X - point.X;
                relatePoint.X = relatePoint.X - 1; ;// 左移移
            }
            else
            {
                if (center.X - _size.Width / 2 < 50)
                {
                    double xx = point.X - center.X;
                    relatePoint.X += 1;// 右移
                }
            }

            if (point.Y <= center.Y)
            {
                // if (_size.Height - center.Y < 50)
                relatePoint.Y += 1;
            }
            else
            {
                // if (center.Y - _size.Height < 50)
                relatePoint.Y -= 1;
            }

            this.Refresh();
        }

        private const double itemMargin = 10;
        Size _size;
        Size actualItemSize = new Size();
        double radius = 1600;
        Point center = new Point(0, 0); //屏幕中心位置
        Point relatePoint = new Point(0, 0);
        double margin = 5;

        #region 属性

        public int RowsCount
        {
            get { return (int)GetValue(RowsCountProperty); }
            set { SetValue(RowsCountProperty, value); }
        }
        public static readonly DependencyProperty RowsCountProperty =
            DependencyProperty.Register("RowsCount", typeof(int), typeof(MyPanel), new PropertyMetadata(4));

        #endregion

        protected override Size MeasureOverride(Size availableSize)
        {
            base.MeasureOverride(availableSize);
            return availableSize;
        }
        protected override Size ArrangeOverride(Size finalSize)
        {
            _size = finalSize;
            this.Refresh();
            base.ArrangeOverride(finalSize);
            return finalSize;
        }

        private void Refresh()
        {
            if (_size.Height < 100) return;

            int count = this.Children.Count;

            #region 元素自适应后的尺寸

            actualItemSize.Height = _size.Height / RowsCount - margin;//适应后的高度
            actualItemSize.Width = actualItemSize.Height;

            #endregion


            //初始化中心位置

            center.X = _size.Width / 2;
            center.Y = _size.Height / 2;

            if (relatePoint.X == 0 && relatePoint.Y == 0)
            {
                relatePoint.X = _size.Width / 2;
                relatePoint.Y = _size.Height / 2;
            }

            int index = 0;
            foreach (FrameworkElement child in this.Children)
            {
                CompositeTransform ct = new CompositeTransform();
                ct.CenterX = 0.5;
                ct.CenterY = 0.5;

                child.RenderTransform = ct;
                ChildElement ce = new ChildElement(index++, child, new Size(_size.Width, _size.Height), relatePoint, actualItemSize, RowsCount, margin, radius);
                if (ce.IsVisible)
                    //都固定在中间位置，但是Projection不同
                    ce.Element.Arrange(new Rect((_size.Width - actualItemSize.Width) / 2, (_size.Height - actualItemSize.Height) / 2, actualItemSize.Width, actualItemSize.Height));
            }
        }
    }
}
